﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net.Nntp
{
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Text;

    /// <summary>Provides helper methods to create fake NNTP servers.</summary>
    internal static class NntpFakeServerHelper
    {
        private static readonly byte[] AvailablePostingAllowedGreeting =
            Encoding.UTF8.GetBytes("200 Posting allowed\r\n");

        private static readonly byte[] AvailablePostingProhibitedGreeting =
            Encoding.UTF8.GetBytes("201 Posting prohibited\r\n");

        private const string UnavailableGreeting = "{0} Service unavailable\r\n";
        private static readonly byte[] ModeReaderCommand = Encoding.UTF8.GetBytes("MODE READER\r\n");

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The caller is responsible for disposal.")]
        internal static FakeServer MakeAvailableServer(bool postingAllowed)
        {
            byte[] greeting = postingAllowed ? AvailablePostingAllowedGreeting : AvailablePostingProhibitedGreeting;
            var result = new FakeServer(Encoding.UTF8, 119, greeting, false);
            result.QueueCommand(ModeReaderCommand, greeting);
            return result;
        }

        internal static FakeServer MakeUnavailableServer(int responseCode)
        {
            byte[] greeting = Encoding.UTF8.GetBytes(
                string.Format(CultureInfo.InvariantCulture, UnavailableGreeting, responseCode));
            FakeServer result = new FakeServer(Encoding.UTF8, 119, greeting, true);
            return result;
        }
    }
}
